package whu.cn.whushare.ui.main.my;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.androidplus.util.StringUtil;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.Result;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.MySettingReShowFragment;
import whu.cn.whushare.ui.main.MainTabActivity;
import whu.cn.whushare.ui.main.MyTabFavoriteActivity;
import whu.cn.whushare.ui.main.MyTabGroupActivity;
import whu.cn.whushare.ui.main.MyTabRelationActivity;
import whu.cn.whushare.ui.main.MyTabSettingActivity;
import whu.cn.whushare.util.EncrypAES;

/**
 * Created by xiaoping on 2015/1/12.
 */
public class SettingPasswordFragment extends MySettingReShowFragment implements View.OnClickListener{
    @ViewMapping(id = R.id.edit_start_password)
    private EditText mSPassword;
    @ViewMapping(id = R.id.edit_end_password)
    private EditText mEPassword;
    @ViewMapping(id = R.id.edit_sure_password)
    private EditText mESPassword;
    @ViewMapping(id=R.id.Finish)
    private Button mFinish;
    private UserManager mUserManager;
    private User mUser;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_setting_password , container, false);//container 为父控件

        initViews(root);
        initActions();
        return root;
    }
    private void initActions() {
        mUserManager = UserManager.getInstance(getActivity());
        mUser = mUserManager.getCurrentUser();
        mFinish.setOnClickListener(this);



    }
    private void initViews(View root) {
        ViewMappingUtil.mapView(this, root);//用于在类上映射layout

      //  mTopBar.title.setText(R.string.password_setting);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Finish:
                String sPass=mSPassword.getText().toString();
                String ePass=mEPassword.getText().toString();
                String eSPass=mESPassword.getText().toString();
                String pass=mUser.getPassword();

                if(pass.equals(sPass))
                {
                if (validatePass(ePass)&&ePass.equals(eSPass)) {

                    Downloader.getInstance(getActivity()).setPassword(sPass,ePass,this);




                }
                    else
                    toast("请确保两次输入的修改密码相同！");
                }
                else{
                    toast("密码输入有误");
                }

            default:
                break;
        }
    }
    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, Result<T> response, Object obj) {
        super.onSuccess(type, response, obj);

        switch (type) {
            case EDIT_PASSWORD:
                toast("修改成功!");
                Downloader.getInstance(getActivity()).login(mUser.getId(),mESPassword.getText().toString(),1,this,mESPassword.getText().toString());
                break;
            case LOGIN_URL:
                mUser = (User) response.getContent();
                mUser.setPassword(EncrypAES.getInstance().DecryptorToString(mUser.getPassword()));
                mUserManager.setCurrentUser(mUser);
                startActivity(MainTabActivity.getStartIntent(getActivity()));

                break;
            default:
                break;

        }

    }
    private boolean validatePass(String ePass) {
        String msg;
        msg = mUserManager.validatePass(ePass).getMsg();
        if (validateMsg(msg)) return false;
        return !validateMsg(msg);
    }

    private boolean validateMsg(String msg) {
        if (!StringUtil.isNullOrEmpty(msg)) {
            toast(msg);
            return true;
        }
        return false;
    }
}

